package cn.edu.suda.schoolBus.service.impl;

import java.sql.Time;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.suda.schoolBus.bean.CarNumber;
import cn.edu.suda.schoolBus.bean.CarNumberExample;
import cn.edu.suda.schoolBus.dao.CarNumberMapper;
import cn.edu.suda.schoolBus.service.CarNumberService;
import cn.edu.suda.schoolBus.util.ResultUtil;

@Service
public class CarNumberServiceImpl implements CarNumberService {

	@Autowired
	private CarNumberMapper carNumberDao;

	private CarNumberExample carNumberExample;

	@Override
	public List<CarNumber> getCarNumber() {
		carNumberExample = new CarNumberExample();
		carNumberExample.createCriteria().andCnIdIsNotNull().andCnStatusEqualTo(1);
		return carNumberDao.selectByExample(carNumberExample);
	}

	@Override
	public int addCarNumber(CarNumber carNumber) {
		carNumber.setCnTime(new Date());
		return carNumberDao.insertSelective(carNumber);
	}

	@Override
	public Map<String, Object> getCarNumer(int page, int rows) {
		int start = (page - 1) * rows;
		carNumberExample = new CarNumberExample();
		carNumberExample.createCriteria().andCnIdIsNotNull();
		Map<String, Object> map = new HashMap<>();
		map.put("total", carNumberDao.countByExample(carNumberExample));
		map.put("rows", carNumberDao.selectByPage(start, rows));
		return map;
	}

	@Override
	public int deleteCarNumber(String ids) {
		carNumberExample = new CarNumberExample();
		carNumberExample.createCriteria().andCnIdIn(ResultUtil.getResult(ids));
		return carNumberDao.deleteByExample(carNumberExample);
	}

	@Override
	public int editCarNumber(CarNumber carNumber) {
		return carNumberDao.updateByPrimaryKeySelective(carNumber);
	}

	@Override
	public int getCarNumber(Time departureTime) {
		carNumberExample = new CarNumberExample();
		carNumberExample.createCriteria().andDepartureTimeEqualTo(departureTime);
		return carNumberDao.selectByExample(carNumberExample).size();
	}

}
